/*
 * FaceCat图形通讯框架
 * 著作权编号:2015SR229355+2020SR0266727
 * 上海卷卷猫信息技术有限公司
 */

using System;
using System.Collections.Generic;

namespace FaceCat {
    /// <summary>
    /// 布局视图
    /// </summary>
    public class FCLayoutDiv : FCDiv {
        /// <summary>
        /// 创建布局视图
        /// </summary>
        public FCLayoutDiv() {
        }

        public bool m_autoWrap = false;

        /// <summary>
        /// 获取是否自动换行
        /// </summary>
        public virtual bool autoWrap()
        {
            return m_autoWrap;
        }

        /// <summary>
        /// 设置是否自动换行
        /// </summary>
        public virtual void setAutoWrap(bool autoWrap)
        {
            m_autoWrap = autoWrap;
        }

        public FCLayoutStyle m_layoutStyle = FCLayoutStyle.LeftToRight;

        /// <summary>
        /// 获取排列模式
        /// </summary>
        public virtual FCLayoutStyle getLayoutStyle()
        {
            return m_layoutStyle;
        }

        /// <summary>
        /// 设置排列模式
        /// </summary>
        public virtual void setLayoutStyle(FCLayoutStyle value)
        {
            m_layoutStyle = value;
        }

        /// <summary>
        /// 获取视图类型
        /// </summary>
        /// <returns>视图类型</returns>
        public override String getViewType() {
            return "LayoutDiv";
        }

        /// <summary>
        /// 获取属性值
        /// </summary>
        /// <param name="name">属性名称</param>
        /// <param name="value">返回属性值</param>
        /// <param name="type">返回属性类型</param>
        public override void getAttribute(String name, ref String value, ref String type) {
            switch (name)
            {
                case "autowrap":
                    type = "bool";
                    value = FCTran.boolToStr(autoWrap());
                    break;
                case "layoutstyle":
                    type = "enum:FCLayoutStyle";
                    value = FCTran.layoutStyleToStr(getLayoutStyle());
                    break;
                default:
                    base.getAttribute(name, ref value, ref type);
                    break;
            }
        }

        /// <summary>
        /// 获取属性名称列表
        /// </summary>
        /// <returns>属性名称列表</returns>
        public override ArrayList<String> getAttributeNames() {
            ArrayList<String> attributeNames = base.getAttributeNames();
            attributeNames.AddRange(new String[] { "AutoWrap", "LayoutStyle" });
            return attributeNames;
        }

        /// <summary>
        /// 重置布局
        /// </summary>
        public virtual bool onResetLayout() {
            bool reset = false;
            if (getNative() != null) {
                FCPadding padding = getPadding();
                int left = padding.left, top = padding.top;
                int width = getWidth() - padding.left - padding.right;
                int height = getHeight() - padding.top - padding.bottom;
                int viewSize = m_views.size();
                int vPos = 0;
                for (int i = 0; i < viewSize; i++) {
                    FCView view = m_views.get(i);
                    if (view.isVisible() && view != getHScrollBar() && view != getVScrollBar()) {
                        FCSize size = view.getSize();
                        int cLeft = view.getLeft(), cTop = view.getTop(), cWidth = size.cx, cHeight = size.cy;
                        int nLeft = cLeft, nTop = cTop, nWidth = cWidth, nHeight = cHeight;
                        FCPadding margin = view.getMargin();
                        switch (m_layoutStyle) {
                            //自下而上
                            case FCLayoutStyle.BottomToTop: {
                                if (vPos == 0)
                                {
                                        top = height - padding.top;
                                    }
                                    int lWidth = 0;
                                    if (m_autoWrap) {
                                        lWidth = size.cx;
                                        int lTop = top - margin.top - cHeight - margin.bottom;
                                        if (lTop < padding.top) {
                                            if (vPos != 0)
                                            {
                                                left += cWidth + margin.left;
                                            }
                                            top = height - padding.top;
                                        }
                                    }
                                    else {
                                        lWidth = width - margin.left - margin.right;
                                    }
                                    top -= cHeight + margin.bottom;
                                    nLeft = left + margin.left;
                                    nWidth = lWidth;
                                    nTop = top;
                                    break;
                                }
                            //从左向右
                            case FCLayoutStyle.LeftToRight: {
                                    int lHeight = 0;
                                    if (m_autoWrap) {
                                        lHeight = size.cy;
                                        int lRight = left + margin.left + cWidth + margin.right;
                                        if (lRight > width) {
                                            left = padding.left;
                                            if (vPos != 0)
                                            {
                                                top += cHeight + margin.top;
                                            }
                                        }
                                    }
                                    else {
                                        lHeight = height - margin.top - margin.bottom;
                                    }
                                    left += margin.left;
                                    nLeft = left;
                                    nTop = top + margin.top;
                                    nHeight = lHeight;
                                    left += cWidth + margin.right;
                                    break;
                                }
                            //从右向左
                            case FCLayoutStyle.RightToLeft: {
                                if (vPos == 0)
                                {
                                        left = width - padding.left;
                                    }
                                    int lHeight = 0;
                                    if (m_autoWrap) {
                                        lHeight = size.cy;
                                        int lLeft = left - margin.left - cWidth - margin.right;
                                        if (lLeft < padding.left) {
                                            left = width - padding.left;
                                            if (vPos != 0)
                                            {
                                                top += cHeight + margin.top;
                                            }
                                        }
                                    }
                                    else {
                                        lHeight = height - margin.top - margin.bottom;
                                    }
                                    left -= cWidth + margin.left;
                                    nLeft = left;
                                    nTop = top + margin.top;
                                    nHeight = lHeight;
                                    break;
                                }
                            //自上而下
                            case FCLayoutStyle.TopToBottom: {
                                    int lWidth = 0;
                                    if (m_autoWrap) {
                                        lWidth = size.cx;
                                        int lBottom = top + margin.top + cHeight + margin.bottom;
                                        if (lBottom > height) {
                                            if (vPos != 0)
                                            {
                                                left += cWidth + margin.left + margin.right;
                                            }
                                            top = padding.top;
                                        }
                                    }
                                    else {
                                        lWidth = width - margin.left - margin.right;
                                    }
                                    top += margin.top;
                                    nTop = top;
                                    nLeft = left + margin.left;
                                    nWidth = lWidth;
                                    top += cHeight + margin.bottom;
                                    break;
                                }
                        }
                        //设置区域
                        if (cLeft != nLeft || cTop != nTop || cWidth != nWidth || cHeight != nHeight) {
                            FCRect rect = new FCRect(nLeft, nTop, nLeft + nWidth, nTop + nHeight);
                            view.setBounds(rect);
                            reset = true;
                        }
                        vPos++;
                    }
                }
            }
            return reset;
        }

        /// <summary>
        /// 设置属性
        /// </summary>
        /// <param name="name">属性名称</param>
        /// <param name="value">属性值</param>
        public override void setAttribute(String name, String value)
        {
            switch (name)
            {
                case "autowrap":
                    setAutoWrap(FCTran.strToBool(value));
                    break;
                case "layoutstyle":
                    setLayoutStyle(FCTran.strToLayoutStyle(value));
                    break;
                default:
                    base.setAttribute(name, value);
                    break;
            }
        }

        /// <summary>
        /// 布局更新方法
        /// </summary>
        public override void update() {
            if (m_native != null && !m_native.m_lockUpdate)
            {
                onResetLayout();
                int viewsSize = m_views.size();
                for (int i = 0; i < viewsSize; i++)
                {
                    m_views.get(i).update();
                }
                updateScrollBar();
            }
        }
    }
}
